package com.talent.jiwen_teacher;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.ui.widgets.LoadDialog;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.Validators;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by qyqx on 2018/8/11.
 * 上传在职证明
 */

public class UpJobEmploymentActivity extends BaseActivity {

    @BindView(R.id.img_cert)
    ImageView img_cert;
    @BindView(R.id.up_photo)
    Button up_photo;

    @BindView(R.id.img_li)
    ImageView img_li;


    private String imgUrl;
    public static final String IMG_JOB = "IMG_JOB";


    private List<LocalMedia> pathList = new ArrayList<>();//选择的照片路径
    private List<String> imgList = new ArrayList<>();//压缩后的图片显示路径

    @Override
    protected int provideContentViewId() {
        return R.layout.activirty_upjob;
    }

    @Override
    public String titleName() {
        return "在职证明";
    }

    @Override
    protected void initData() {

        mBtnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra(IMG_JOB, imgUrl);
                setResult(1005, intent);
                finish();

            }
        });


        imgUrl=getIntent().getStringExtra(IMG_JOB);
        if(!Validators.isEmpty(imgUrl)){
            img_li.setVisibility(View.GONE);
            ImageUtil.loadImageWithDefault(mContext, imgUrl, R.mipmap.img_job, img_cert);
        }

    }

    private List<String> showList = new ArrayList<>();
    @OnClick({R.id.img_cert, R.id.up_photo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_cert:
                if(!Validators.isEmpty(imgUrl)){
                    showList.clear();
                    showList.add(imgUrl);
                    ImageUtil.picturePreviewWithUrl(UpJobEmploymentActivity.this,0,showList);
                }
                break;
            case R.id.up_photo:
                ImageUtil.pickPhoto(UpJobEmploymentActivity.this, 1, pathList, true);
                break;

        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(IMG_JOB, imgUrl);
        setResult(1005, intent);
        finish();
        super.onBackPressed();
    }


    String imgPath;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    pathList.clear();
                    pathList.addAll(PictureSelector.obtainMultipleResult(data));

                    imgList.clear();
                    for (LocalMedia localMedia : pathList) {
                        imgList.add(localMedia.getCompressPath());
                    }
                    if (!Validators.isEmpty(imgList) && imgList.size() > 0) {
                        imgPath = imgList.get(0);

                        if (!Validators.isEmpty(imgPath)) {
                            LoadDialog.show(UpJobEmploymentActivity.this);
                            ImageUtil.upLoadSingleImg(imgPath, new ImageUtil.ImgUpLoadListener() {
                                @Override
                                public void OnSuccess(String successUrl) {
                                    imgUrl = successUrl;
                                    img_li.setVisibility(View.GONE);
                                    showToast("上传成功");
                                    ImageUtil.loadImageWithDefault1(mContext, imgUrl, R.mipmap.img_job, img_cert);
                                    LoadDialog.dismiss(UpJobEmploymentActivity.this);
//                                    Intent intent = new Intent();
//                                    intent.putExtra(IMG_CERT, imgUrl);
//                                    setResult(1001, intent);
//                                    finish();

                                }

                                @Override
                                public void OnError(String message) {
                                    showToast("上传失败，请重新上传");
                                    LoadDialog.dismiss(UpJobEmploymentActivity.this);
                                }

                                @Override
                                public void OnListUpSuccess(List<String> successFileList) {

                                }
                            });
                        }else{
                            showToast("请先选择图片");
                        }
                    }
                    break;
            }
        }
    }


}
